package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.springframework.context.annotation.Scope;


import academic.concordia.bankofquebec.bean.bill.IBillBean;
import academic.concordia.bankofquebec.bean.billprovider.BillProviderService;
import academic.concordia.bankofquebec.bean.billprovider.IBillProviderService;
import academic.concordia.bankofquebec.bean.client.IClientBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.Bill;
import academic.concordia.bankofquebec.entity.BillProvider;

@ManagedBean(name="billManagedBean")
@Scope("session")
public class BillManagedBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Bill bill;
	private Map<String, String> billProviders;
	
	private IBillProviderService billProviderService;
	
	@ManagedProperty(value="#{BillBean}")
	IBillBean billBean;
	
	@ManagedProperty(value="#{ClientBean}")
	IClientBean clientBean;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;
	
	@PostConstruct
    public void init() {
		bill = new Bill();
		constructBillProviders();
    }
	
	public void constructBillProviders() {
		billProviders = new LinkedHashMap<String, String>();
		for(BillProvider billProvider : loadBillProviders()) {
			billProviders.put(billProvider.getId(), billProvider.getName());
		}
	}
	
	public List<BillProvider> loadBillProviders() {
		billProviderService = new BillProviderService();
		return billProviderService.getBillProviders();
	}

	public String subscribeBill() {
		
		bill.setClient(sessionData.getClient());
		billBean.createBill(bill);
		return "products";
	}

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public Map<String, String> getBillProviders() {
		return billProviders;
	}

	public void setBillProviders(Map<String, String> billProviders) {
		this.billProviders = billProviders;
	}

	public IBillProviderService getBillProviderService() {
		return billProviderService;
	}

	public void setBillProviderService(IBillProviderService billProviderService) {
		this.billProviderService = billProviderService;
	}

	public IBillBean getBillBean() {
		return billBean;
	}

	public void setBillBean(IBillBean billBean) {
		this.billBean = billBean;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public IClientBean getClientBean() {
		return clientBean;
	}

	public void setClientBean(IClientBean clientBean) {
		this.clientBean = clientBean;
	}
}
